package com.yanqu.road.server.cmd.building;

import com.yanqu.road.entity.player.UserBuilding;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.building.BuildingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BuildingModule;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.S_BUILDING_ADD_STAFF, desc = "店铺招募员工")
public class BuildingAddStaffCmd extends AbstractCommand {

    private int buildingId;

    private int addStaffCount;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BuildingProto.BuildingAddStaffReqMsg reqMsg = BuildingProto.BuildingAddStaffReqMsg.parseFrom(packet.getBytes());
        addStaffCount = reqMsg.getStaffCount();
        buildingId = reqMsg.getBuildingId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(addStaffCount > 1){
            if(player.getTitleId() < GameConfig.TEN_RECRUIT_STAFF_NEED_TITLE){
                ret = GameErrorCode.E_BUILDING_TEN_RECRUIT_TITLE_NO_ENOUGH;
                return;
            }
            long staffCount = 0L;
            for (Map.Entry<Integer, UserBuilding> buildingEntry : player.getModule(BuildingModule.class).getBuildingMap().entrySet()) {
                staffCount += buildingEntry.getValue().getStaffCount();
            }
            if(staffCount < GameConfig.TEN_RECRUIT_STAFF_NEED_RECRUIT_NUM){
                ret = GameErrorCode.E_BUILDING_TEN_RECRUIT_RECRUIT_NUM_NO_ENOUGH;
                return;
            }
        }
        ret = player.getModule(BuildingModule.class).addStaff(buildingId, addStaffCount);
    }


    @Override
    public void sendData(GamePlayer player) {
        BuildingProto.BuildingRespMsg.Builder respMsg = BuildingProto.BuildingRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_BUILDING_ADD_STAFF, respMsg);
    }
}
